package com.yiren.dbaa.module.device.model;

import android.app.Activity;

import com.yiren.backstage.communication.api.impl.SerialCommunicationApiImpl;
import com.yiren.backstage.communication.interfaces.DataCallback;
import com.yiren.backstage.communication.utils.MessagesUtil;
import com.yiren.backstage.config.api.LoadConfigApi;
import com.yiren.backstage.config.bean.device.SignalSourceConfigBean;
import com.yiren.backstage.currency.bean.CarStateBean;
import com.yiren.backstage.currency.enums.ObdCarModelEnum;
import com.yiren.dbaa.constant.G;
import com.yiren.dbaa.currency.model.ObdCarModel;
import com.yiren.dbaa.module.device.contract.LowerComputerContract;
import com.yiren.dbaa.module.param_config.api.ParamConfigApi;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by WANGRONGXIANG
 * on 2022/6/21
 * Describe OBD控制  command
 */
public class LowerComputerModel implements LowerComputerContract.Model {

    private Activity activity;

    public LowerComputerModel(Activity activity) {
        this.activity = activity;

    }

    @Override
    public String getObdName() {
        SignalSourceConfigBean signalSourceConfig = LoadConfigApi.loadSignalSourceConfig(activity);
        Integer obdModel = signalSourceConfig.getObdModel();
        String obdName = "";
        if (null != obdModel) {
            if (obdModel == 0 && null != G.connectDeviceMessage && (G.connectDeviceMessage.getVersionDescribe().contains("YR") || G.connectDeviceMessage.getVersionDescribe().contains("XQ") || G.connectDeviceMessage.getVersionDescribe().contains("WP"))) {
                obdName = "接线版";
            } else {
                ObdCarModelEnum[] obdCarModelCodes = ObdCarModelEnum.values();
                for (int i = 0; i < obdCarModelCodes.length; i++) {
                    if (obdModel == (obdCarModelCodes[i].getNameCode())) {
                        obdName = obdCarModelCodes[i].getName();
                    }
                }
            }
        } else {
            //为空时保存盒子的车型
            obdModel = Integer.parseInt(CarStateBean.getInstance().getObdCarModel());
            signalSourceConfig.setObdModel(obdModel);
            ParamConfigApi.saveSignalSourceConfig(activity, signalSourceConfig);
            getObdName();
        }
        return obdName;
    }

    @Override
    public void switchCarModel(int obdCarModelCode) {
        MessagesUtil message = new MessagesUtil(MessagesUtil.OBD_MODELS, 1, MessagesUtil.toLH(obdCarModelCode), new byte[]{});
        SerialCommunicationApiImpl.getInstance(activity).writeData(message.getMessage(), "MP", new DataCallback() {
            @Override
            public void onSendReadData(String data) {
                super.onSendReadData(data);
            }
        });
    }

    @Override
    public void saveCarModel(int obdCarModelCode) {
        SignalSourceConfigBean signalSourceConfig = LoadConfigApi.loadSignalSourceConfig(activity);
        signalSourceConfig.setObdModel(obdCarModelCode);
        ParamConfigApi.saveSignalSourceConfig(activity, signalSourceConfig);
    }

    @Override
    public List<ObdCarModel> getObdCarModels() {
        List<ObdCarModel> obdCarModels = new ArrayList<>();
        if (null != G.connectDeviceMessage && (G.connectDeviceMessage.getVersionDescribe().contains("YR") || G.connectDeviceMessage.getVersionDescribe().contains("XQ") || G.connectDeviceMessage.getVersionDescribe().contains("WP"))) {  //自研产品有主机盒功能则加入接线板车型切换
            ObdCarModel obdCarModel0 = new ObdCarModel();
            obdCarModel0.setName("接线版");
            obdCarModel0.setNameCode(0);
            obdCarModels.add(obdCarModel0);
        }
        ObdCarModelEnum[] obdCarModelCodes = ObdCarModelEnum.values();
        for (int i = 0; i < obdCarModelCodes.length; i++) {
            ObdCarModel obdCarModel = new ObdCarModel();
            obdCarModel.setName(obdCarModelCodes[i].getName());
            obdCarModel.setNameCode(obdCarModelCodes[i].getNameCode());
            obdCarModels.add(obdCarModel);
        }
        return obdCarModels;
    }

}